home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c++-part1 / 7026 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  5.0 KB

  1. Path: linus.mitre.org!spectre!eachus
  2. From: eachus@spectre.mitre.org (Robert I. Eachus)
  3. Newsgroups: comp.lang.ada,comp.lang.c++
  4. Subject: Re: on OO differnces between Ada95 and C++
  5. Date: 20 Feb 1996 19:15:14 GMT
  6. Organization: The Mitre Corp., Bedford, MA.
  7. Message-ID: <EACHUS.96Feb20141514@spectre.mitre.org>
  8. References: <4gbq7q$g08@qualcomm.com>
  9. NNTP-Posting-Host: spectre.mitre.org
  10. In-reply-to: nabbasi@qualcomm.com's message of 20 Feb 1996 06:37:46 GMT
  11.  
  12. In article <4gbq7q$g08@qualcomm.com> nabbasi@qualcomm.com (Nasser Abbasi) writes:
  13.  
  14.   > I have been playing around with the OO features in Ada95 and
  15.   > comparing it with C++. I noticed this little difference, and I'd
  16.   > like to see what you think of it...
  17.  
  18.   > In C++, the client to the saving_account class can also use the
  19.   > Money_Type type (even though that is defined in the base class
  20.   > Account) without having to include base class "account.h", this is
  21.   > because Money_Type has become a public part of the Saving_Account
  22.   > class when Saving_Account inherited Saving class.
  23.  
  24.     Not quite true. The visibility is because the copy semantics
  25. associated with #include are transitive.  Anyone who includes
  26. saving_account also copies account, and in fact the "standard" C
  27. idiom of defining a global and testing it around #includes is to avoid
  28. including multiple copies.
  29.  
  30.   > In Ada95, the client of Saving_Account has no viability to Money_Type
  31.   > type definition even though they with'ed Saving_account package, since
  32.   > Money_Type is not a "inherited" by the Saving_Account package
  33.   > from the Account package.
  34.  
  35.     Again it is a little more complex than that.  In Ada 95 the type is
  36. in scope throughout all of the semantic dependents, but the name of
  37. the type or of its operations is visible in a subset of that range.
  38. In any case there are several different ways of making visible (or
  39. directly visible) the name and operations of Money_Type.
  40.  
  41.   > This means that in Ada95, If one wants to access things like type
  42.   > definitions that are not tagged, but used in defining components
  43.   > inside the tagged record, one must "with" the client package and
  44.   > also packages that the client package with'ed just to be able to
  45.   > have viability to those type definitions.
  46.  
  47.     No, another solution is to re-export them from Saving_Account.
  48. That is seldom the right solution in Ada, and is probably not right
  49. here. 
  50.  
  51.   > In this case, It seems the C++ way of having everything inside the
  52.   > public base class becoming visible to clients of the derived class
  53.   > that inherits the base class is more economical?.
  54.  
  55.   > ...Any better way?
  56.  
  57.      Yes, 1) restructure your program to use child packages:
  58.  
  59.    ----------------- accounting.ads
  60.  
  61.    package Accounting is
  62.  
  63.      type Money_Type is delta 0.01 digits 9; -- just my example ;-)
  64.      type Account_Id is private;
  65.  
  66.    private
  67.  
  68.      type Account_Id is new Integer;
  69.  
  70.    end Accounting;
  71.  
  72.    ------------------ account.ads ---------------------------
  73.  
  74.    package Accounting.Account is
  75.  
  76.      type Account_Type is tagged private;
  77.   
  78.      -- operations on type Account_Type defined here including
  79.  
  80.      procedure Set_Balance(A: Account, M: Accounting.Money_Type);
  81.  
  82.    private
  83.  
  84.      function New_Account_ID return Account_ID;
  85.  
  86.      type Account_Type is tagged
  87.       record
  88.     Balance    : Money_Type := 0.0;
  89.     Id         : Account_Id := New_Account_ID;
  90.       end record;
  91.  
  92.    end Account;
  93.  
  94.    ----------------- saving_account.ads ----------------
  95.  
  96.    with Accounting.Account;
  97.    package Saving_Account is
  98.  
  99.     type Saving_Account_Type is new Account.Account_Type with private;
  100.  
  101.     -- new operations on savings accounts defined here including
  102.  
  103.    private
  104.  
  105.     type Saving_Account_Type is new Account.Account_Type with
  106.       record
  107.     Interest : Account.Money_Type;
  108.       end record;
  109.  
  110.    end Saving_Account;
  111.  
  112.    --------------- main.adb ---------------------------
  113.  
  114.    with Accounting.Saving_Account;
  115.    -- with of Accounting is implicit, with of Accounting.Acount unneeded.
  116.    with Ada.Text_IO; use Ada.Text_IO;
  117.  
  118.    procedure main is       
  119.      package Money_IO is new Ada.Text_IO.Decimal_IO(Accounting.Money_Type);
  120.      The_Saving_Account : Saving_Account.Saving_Account_Type;
  121.      The_Balance : Accounting.Money_Type; 
  122.    begin
  123.        Money_IO.Get(The_Balance);
  124.        Set_Balance(The_Saving_Account, The_Balance);
  125.    end Main;
  126.  
  127.    I added the use of private parts to emphasize the way this works.
  128. The operations declared explicitly or implicitly within the private
  129. part of Accounting are visible within the children of Accounting as
  130. well.  So I could, if I wanted, hide the full declaration of
  131. Money_Type, and the instantiation of Money_IO within the private part
  132. of Accounting, and explicitly export operations which do IO from
  133. Accounting.Account.  Of course this would implicitly define the same
  134. operations for Saving_Account and/or I could add additional IO
  135. operations.
  136.  
  137.     All in all a bit heavy for such a small example.  But in larger
  138. programs, you find yourself pushing for information hiding wherever
  139. possible.
  140.  
  141. --
  142.  
  143.                     Robert I. Eachus
  144.  
  145. with Standard_Disclaimer;
  146. use  Standard_Disclaimer;
  147. function Message (Text: in Clever_Ideas) return Better_Ideas is...
  148.